package com.guigu.www.tangyingmeng.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guigu.www.denghao.service.DHMemberService;
import com.guigu.www.mapper.*;
import com.guigu.www.pojo.*;
import com.guigu.www.tangyingmeng.service.TymSchemeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
@Service
public class TymSchemeServiceImpl implements TymSchemeService {

    @Autowired
    private BespeakResultMapper resultMapper;
    @Autowired
    private DiseaseMapper diseaseMapper;
    @Autowired
    private BespeakMapper bespeakMapper;
    @Autowired
    private DHMemberService memberService;
    @Autowired
    private BespeakSetMealMapper setMealMapper;
    @Autowired
    private ConstitutionTestMapper constitutionTestMapper;
    @Autowired
    private MindTestMapper mindTestMapper;
    @Autowired
    private RiskTestMapper riskTestMapper;
    @Autowired
    private FoodZongMapper foodMapper;
    @Autowired
    private KeepZongMapper keepMapper;
    @Autowired
    private TemplateWholesomeMapper wholesomeMapper;
    @Autowired
    private InterventionTemplateMapper templateMapper;
    @Autowired
    private InterventionMapper interventionMapper;

    @Override
    public PageVo<BespeakResult> selschemeall(Map<String, String> map) {
        QueryWrapper<BespeakResult> wrapper = new QueryWrapper<>();
        wrapper.like("bes_result_id",map.get("findid")).like("bes_result_name", map.get("findname")).eq("bes_result_confirm",1).eq("bes_result_health",1);
        IPage<BespeakResult> page = new Page<>(Integer.valueOf(map.get("index")), Integer.valueOf(map.get("pageSize")));
        page = resultMapper.selectPage(page, wrapper);
        PageVo<BespeakResult> pageVo = new PageVo<>();
        pageVo.setTotal(page.getTotal());
        pageVo.setRows(page.getRecords());
        for (BespeakResult result : pageVo.getRows()) {
            result.setDisease(diseaseMapper.selectById(result.getBesResultNormal()));
        }
        return pageVo;
    }


    //查询单个患者信息
    @Override
    public BespeakResult selschemeone(Integer id) {
        BespeakResult result = resultMapper.selectById(id);
        result.setBespeak(selbespeak(result.getBesId()));
        result.setMember(selmember(result.getBesId()));
        result.setSetMeal(selsetmeal(result.getBesId()));
        if(result.getBesResultNormal() != 0){
            result.setDisease(seldisease(result.getBesResultNormal()));
        }else {
            result.setDisease(null);
        }
        result.setConstitutionTest(selconstitutionTest(result.getMember().getMemId()));
        result.setMindTest(selmindTest(result.getMember().getMemId()));
        result.setRiskTest(selrisktest(result.getMember().getMemId()));
        return result;
    }

    //查询所有干预方案模板
    @Override
    public List<InterventionTemplate> seltemplateall() {
        QueryWrapper<InterventionTemplate> wrapper = new QueryWrapper<>();
        wrapper.eq("inter_temp_state",0);
        List<InterventionTemplate> list = templateMapper.selectList(wrapper);
        return list;
    }

    //查询所有膳食方案
    @Override
    public List<FoodZong> selfoodall(){
        QueryWrapper<FoodZong> wrapper = new QueryWrapper<>();
        wrapper.eq("food_zong_state",0);
        List<FoodZong> list = foodMapper.selectList(wrapper);
        return list;
    }

    //查询所有运动方案
    @Override
    public List<KeepZong> selkeepall(){
        QueryWrapper<KeepZong> wrapper = new QueryWrapper<>();
        wrapper.eq("keep_zong_state",0);
        List<KeepZong> list = keepMapper.selectList(wrapper);
        return list;
    }

    //查询所有健康方案
    @Override
    public List<TemplateWholesome> selwhoall(){
        QueryWrapper<TemplateWholesome> wrapper = new QueryWrapper<>();
        wrapper.eq("temp_who_state",0);
        List<TemplateWholesome> list = wholesomeMapper.selectList(wrapper);
        return list;
    }

    //体检预约表
    @Override
    public Bespeak selbespeak(Integer id) {
        return bespeakMapper.selectById(id);
    }

    //会员及其关系表
    @Override
    public Member selmember(Integer id) {
        Bespeak bespeak = bespeakMapper.selectById(id);
        Member member = memberService.queryByMemIdMember(bespeak.getMemId());
        member.setTrends(memberService.queryByMemIdTrends(bespeak.getMemId()));
        member.setCaseHistory(memberService.queryByMemIdCaseHistory(bespeak.getMemId()));
        return member;
    }

    //体检套餐表
    @Override
    public BespeakSetMeal selsetmeal(Integer id) {
        Bespeak bespeak = bespeakMapper.selectById(id);
        return setMealMapper.selectById(bespeak.getBesSmId());
    }

    //疾病表
    @Override
    public Disease seldisease(Integer id) {
        return diseaseMapper.selectById(id);
    }

    //体质测评表
    @Override
    public ConstitutionTest selconstitutionTest(Integer id) {
        QueryWrapper<ConstitutionTest> wrapper = new QueryWrapper<>();
        wrapper.eq("mem_id",id);
        return constitutionTestMapper.selectOne(wrapper);
    }

    //心理测评表
    @Override
    public MindTest selmindTest(Integer id) {
        QueryWrapper<MindTest> wrapper = new QueryWrapper<>();
        wrapper.eq("mem_id",id);
        return mindTestMapper.selectOne(wrapper);
    }

    //风险评估表
    @Override
    public RiskTest selrisktest(Integer id) {
        QueryWrapper<RiskTest> wrapper = new QueryWrapper<>();
        wrapper.eq("mem_id",id);
        return riskTestMapper.selectOne(wrapper);
    }

    @Override
    public Boolean subscheme(Intervention intervention) {
        Date now = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = dateFormat.format( now );
        intervention.setInterPlanCreateTime(time);
        if (intervention.getTemid() != null) {
            InterventionTemplate template = templateMapper.selectById(intervention.getTemid());
            intervention.setTempFoodId(template.getTempFoodId());
            intervention.setTempKeepId(template.getTempKeepId());
            intervention.setTempWhoId(template.getTempWhoId());
        }
        int insert = interventionMapper.insert(intervention);
        System.out.println(intervention);
        BespeakResult bespeakResult = new BespeakResult();
        bespeakResult.setInterId(intervention.getInterId());
        bespeakResult.setBesResultId(intervention.getBesResultId());
        resultMapper.updateById(bespeakResult);
        if(insert>0){
            return true;
        }else {
            return false;
        }
    }
}
